package com.datastructure.test.simplifypath;

import java.util.Stack;

public class SimplifyPath {

    public static void main(String[] args) {
        SimplifyPath simplifyPath = new SimplifyPath();
        System.out.println(simplifyPath.simplifyPath("/home/web/.ssh/../.ssh2/authorized_keys/"));
    }

    public String simplifyPath (String path) {
        // write code here
        if(path.equals("/..")){
            return "/";
        }
        while (path.contains("//")){
            path = path.replace("//","/");
        }
        while (path.contains("/./")){
            path = path.replace("/./","/");
        }
        int location;
        while (path.contains("/../")){
            location = path.lastIndexOf("/../");
            if(location==0){
                path = path.replace("/../","/");
                break;
            } else {
                while (!Character.isLetter(path.charAt(location))){
                    location--;
                }
                path = doSimplePath(location,path);
            }
        }
        while (path.contains("//")){
            path = path.replace("//","/");
        }
        if(path.length()==1||path.length()==0){
            return path;
        }
        if(path.endsWith("/")){
            path = path.substring(0,path.length()-1);
        }
        if(path.endsWith("/..")){
            path = doSimplePath2(path.lastIndexOf("/..")-1,path);
        }
        if(path.endsWith("/.")){
            path = path.substring(0,path.length()-2);
        }
        if(path.length()==1||path.length()==0){
            return "/";
        }
        return path;
    }

    private String doSimplePath2(int location, String path) {
        StringBuilder stringBuilder = new StringBuilder();
        while (Character.isLetter(path.charAt(location))||path.charAt(location)=='.'){
            stringBuilder.append(path.charAt(location));
            location--;
        }
        stringBuilder.append('/');
        stringBuilder.reverse().append("/..");
        path = path.replace(stringBuilder.toString(),"");
        return path;
    }

    private String doSimplePath(int location, String path) {
        StringBuilder stringBuilder = new StringBuilder();
        while (Character.isLetter(path.charAt(location))||path.charAt(location)=='.'){
            stringBuilder.append(path.charAt(location));
            location--;
        }
        stringBuilder.append('/');
        stringBuilder.reverse().append("/../");
        path = path.replace(stringBuilder.toString(),"/");
        return path;
    }

    public String simplifyPath2 (String path) {
        // write code here
        String[] strs = path.split("/");
        Stack<String> stack = new Stack<>();
        for (String str: strs) {
            if (str.equals("..")) {
                if (!stack.isEmpty()) {
                    stack.pop();
                }
            }
            else if (!(str.equals(".") || str.equals(""))) {
                stack.push(str);
            }
        }
        String res = "";
        while (!stack.isEmpty()) {
            res = "/" + stack.pop() + res;
        }
        return res.length() == 0 ? "/" : res;
    }
}
